<?php

namespace Admin\Controller;

use Base\BackController;

class CalendarController extends BackController {

    //用户列表
    public function index() {
        // echo 1;
        $this->user_model = M('bh_calendar');
        $join = 'left join eosi_weixin_user ON eosi_bh_calendar.uid = eosi_weixin_user.id';
        $joinad ='left join eosi_bh_looks ON eosi_bh_calendar.look_id = eosi_bh_looks.id';
       
        $this->status_arr= array(
            '0' => '无效',
            '1' => '有效'
        );
        $this->istop= array(
            '0' => '否',
            '1' => '是'
        );
        //perry add 2016-06-23 如果当前用户不是admin,则只能查看自己的信息
        $map=array();
        if($_GET['nicknamex']!=''){
            $map['nickname']=array('like', '%'.$_GET['nicknamex'].'%');
        }
        if($_GET['memo']!=''){
             $map['eosi_bh_calendar.memo']=array('like', '%'.$_GET['memo'].'%');;
        }
        if($_GET['bname']!=''){
            // var_dump($_GET['bname']);string '2017-07-07;
            $str =$_GET['bname'];
            $_GET['bname'] = explode('-', $_GET['bname']);
            $a = $_GET['bname'][1];
            $a = preg_replace('/^0/', '', $a);
            $b = $_GET['bname'][2];
            $b = preg_replace('/^0/', '', $b);
             $map['eosi_bh_calendar.year']=array('like',$_GET['bname'][0]);
             $map['eosi_bh_calendar.month']=array('like',$a);
             $map['eosi_bh_calendar.day']=array('like',$b);
             // var_dump($str);
        }
        //任务总数
        $total = $this->user_model->join($join)->join($joinad)->where($map)->count();
        //每页条数
        $listRows = C('LIST_ROWS') > 0 ? C('LIST_ROWS') : 10;
        //实例化分页类
        $page = new \Admin\ORG\PageNew($total, $listRows, '');
        if ($total > $listRows) {
            $page->setConfig('theme', '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
        }
        $p = $page->show();
        $this->assign('_page', $p ? $p : '');
        $ab ='eosi_weixin_user.nickname,eosi_bh_looks.img,eosi_bh_looks.create_time as look_create_time,eosi_bh_calendar.*';
        $dataList = $this->user_model->join($join)->join($joinad)->where($map)->order('eosi_bh_calendar.id desc')->limit("$page->firstRow,$page->listRows")->Field($ab)->select();

        $this->new_type =M('bh_label');
        $dataListopt =$this->new_type->select();
        //数据处理
        foreach ($dataList as $k => $value){
            //状态
            if($value['is_top'] >=1){
                  $value['is_top'] = $this->istop[1];
            }else{
                  $value['is_top'] = $this->istop[0];
            }
           if ($value['status'] == 1) {
                $value['status_str'] = "<span class=\"label label-sm label-success arrowed-in\">" . $this->status_arr[$value['status']] . "</span>&nbsp;|&nbsp;&nbsp;&nbsp;<span class=\"hand icon-lock red bigger-125\" title=\"禁用\" index='" . $value['id'] . "' ></span>";
            }else{
                $value['status_str'] = "<span class=\"label label-sm label-info arrowed-in\">" . $this->status_arr[0] . "</span>&nbsp;|&nbsp;&nbsp;&nbsp;<span class=\"hand icon-unlock green bigger-125\" title=\"启用\"  index='" . $value['id'] . "' ></span>";
            }
            $value['riqi'] = $value['year'].'-'.$value['month'].'-'.$value['day'];
            $value['create_time'] = date('Y-m-d H:i:s', $value['create_time']);
            $value['update_time'] = date('Y-m-d H:i:s', $value['update_time']);
            $value['s_img'] = ic_getUploadImgFullPath('looks',ic_getDiffSizeImg($value['img'],'thumb'),$value['look_create_time']); 
            $value['img'] = ic_getUploadImgFullPath('looks',$value['img'],$value['look_create_time']);
            $dataList[$k] = $value;
        }
        $this->assign('_listopt', $dataListopt);
        $this->assign('_list', $dataList);
        $this->assign('str', $str);
        $this->assign('nicknamex', $_GET['nicknamex']);
        $this->assign('aname', $_GET['aname']);
        $this->assign('memo', $_GET['memo']);
        $this->assign('groupName', '日历');
        $this->assign('moduleName', '日历列表');
        $this->assign('actionName', '列表概览');
        $this->assign('nickname',$array['nickname']);
        $this->display();
    }

    public function status_change(){
        try {
            $type = I('type');
            $userid = I('id');
            if (empty($type) || empty($userid)) {
                $ajaxdata['info'] = '丢失参数';
                $ajaxdata['status'] = 200;
                $this->ajaxReturn($ajaxdata);
            }
            if ($type == '有效')
                $str = '启用用户';
            if ($type == '无效')
                $str = '禁用用户';
            $data = array();
            $data['id'] = $userid;
            if ($type == '有效') {
                $data['status'] = 0;
            }
            if ($type == '无效') {
                $data['status'] = 1;
            }
            $falg=M('bh_calendar')->save($data); 
            if(!$falg){
                $ajaxdata['info'] = '网络故障提交失败,请稍候再试';
                $ajaxdata['status'] = 200;
                $this->ajaxReturn($ajaxdata);
            }
            $ajaxdata['info'] = 'ok';
            $ajaxdata['status'] = 1;
            $this->ajaxReturn($ajaxdata);
        } catch (Exception $exc) {
            $ajaxdata['info'] = $exc->getMessage();
            $ajaxdata['status'] = 400;
            $this->ajaxReturn($ajaxdata);
        }
    }
    // 添加用户
    // public function addUser() {
    //     $this->assign('groupName', '权限管理');
    //     $this->assign('moduleName', '衣橱管理');
    //     $this->assign('actionName', '衣橱列表');
    //     $this->display('CloserCommodity:index');
    // }

    //保存用户
    // public function insertUser() {
    //     $username = I('username');
    //     $password = I('password');
    //     $nickname = I('nickname');
    //     $map = array();
    //     $map['username'] = $username;
    //     $data = M('auth_members')->where($map)->find();
    //     if (!empty($data)) {
    //         $this->error("用户名\"{$username}\"已存在");
    //     }
    //     $saveData = array();
    //     $saveData['username'] = $username;
    //     $saveData['password'] = md5($password);
    //     $saveData['nickname'] = $nickname;
    //     $saveData['create_time'] = time();
    //     $flag = M('auth_members')->add($saveData);
    //     if ($flag) {
    //         $this->success('用户添加成功', __CONTROLLER__ . '/user');
    //     } else {
    //         $this->error('用户添加失败');
    //     }
    // }

    //开启用户|关闭用户
    // public function setUserStatus() {
    //     $type = I('type');
    //     $userid = I('id');
    //     if (empty($type) || empty($userid)) {
    //         $this->error('丢失参数');
    //     }
    //     if ($type == 'open')
    //         $str = '启用用户';
    //     if ($type == 'close')
    //         $str = '禁用用户';

    //     $data = array();
    //     $data['id'] = $userid;
    //     if ($type == 'open') {
    //         $data['status'] = 1;
    //     }
    //     if ($type == 'close') {
    //         $data['status'] = 0;
    //     }
    //     $flag = M('auth_members')->save($data);
    //     if ($flag) {
    //         $this->success("{$str}成功", U('user'));
    //     } else {
    //         $this->error("{$str}失败");
    //     }
    // }

    // 编辑用户
    // public function editUser() {
    //     $userid = I('id');
    //     if (empty($userid)) {
    //         $this->error('缺失主键');
    //     }
    //     $data = M('auth_members')->where("id={$userid}")->find();
    //     if (empty($data)) {
    //         $this->error("用户不存在");
    //     }
    //     $this->assign("data", $data);
    //     $this->assign('groupName', '权限管理');
    //     $this->assign('moduleName', '衣橱管理');
    //     $this->assign('actionName', '衣橱列表');
    //     $this->display('CloserCommodity:index');
       
    // }

    //更新用户
    // public function updateUser() {
    //     $userid = I('id');
    //     if (empty($userid)) {
    //         $this->error("缺失主键");
    //     }
    //     $username = I('username');
    //     $nickname = I('nickname');

    //     $map = array();
    //     $map['username'] = $username;
    //     $map['id'] = array("neq", $userid);
    //     $data = M('auth_members')->where($map)->find();
    //     if (!empty($data)) {
    //         $this->error("用户名\"{$username}\"已存在");
    //     }
    //     $saveData = array();
    //     $saveData['id'] = $userid;
    //     $saveData['username'] = $username;
    //     $saveData['nickname'] = $nickname;
    //     $flag = M('auth_members')->save($saveData);
    //     if ($flag) {
    //         $this->success("用户编辑成功", __CONTROLLER__ . "/user");
    //     } else {
    //         $this->error("用户编辑失败，可能未做修改");
    //     }
    // }

    //删除用户
    // public function deleteUser() {
    //     $userid = I('id');
    //     if (empty($userid)) {
    //         $this->error('缺失主键');
    //     }
    //     //删除用户
    //     $flag = M('auth_members')->where("id={$userid}")->delete();
    //     if ($flag) {
    //         $this->success('删除用户成功', __CONTROLLER__ . '/user');
    //     } else {
    //         $this->error('删除用户失败');
    //     }
    // }

    //修改用户密码
    // public function editPwd() {
    //     $userid = I('id');
    //     $password = I('password');
    //     if (empty($userid)) {
    //         $this->error('缺失主键');
    //     }
    //     $saveData = array();
    //     $saveData['id'] = $userid;
    //     $saveData['password'] = md5($password);
    //     $flag = M('auth_members')->save($saveData);
    //     if ($flag) {
    //         $this->success('修改密码成功', __CONTROLLER__ . '/user');
    //     } else {
    //         $this->error('修改密码失败,可能未做修改');
    //     }
    // }

}
